package com.skytech.component.crawler.web;

import com.alibaba.fastjson.JSONObject;
import com.skytech.component.crawler.scratch.base.CrawlerStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class PolicyNewsController {
    @Autowired
    private CrawlerStorageService crawlerStorageService;

    /**
     * 条件检索
     * @param jsonObject
     * @param response
     * @return
     */
    @PostMapping("crawler/storage/condition")
    public JSONObject search(@RequestBody JSONObject jsonObject, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        String beginTime= jsonObject.getString("beginTime");
        String endTime= jsonObject.getString("endTime");
        String catalog= jsonObject.getString("catalog");
        String keywords= jsonObject.getString("keywords");
        Integer pageSize= jsonObject.getInteger("pageSize");
        Integer pageNo= jsonObject.getInteger("pageNo");
        String year= jsonObject.getString("year");
        return crawlerStorageService.listYearPolicyNews(year,beginTime,endTime,catalog,keywords,pageNo,pageSize,true);
    }
    @GetMapping("crawler/storage/direct/{year}")
    public JSONObject directPolicyNews(@PathVariable String year,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        return crawlerStorageService.listYearPolicyNews(year,"","","","",null,null,false);

    }
    @PostMapping("crawler/storage/delete")
    public boolean delete(@RequestBody List<String> idList,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        return crawlerStorageService.deletePolicy(idList);
    }
    @PostMapping("crawler/storage/save/{year}")
    public boolean saveAll(@RequestBody List<String> idList,@PathVariable String year,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        return crawlerStorageService.saveData(idList,year);
    }
}
